package com.df.bpm.utils.businessViewerV2.informatization;

import com.df.bpm.utils.businessViewerV2.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public class Db_requirement {

    String ctrllerPath="informatization/";

    public M_requirementDetailsDto getDetailsDto(Integer informatizationRequirementIndex) {
        RestTemplate restTemplate = new RestTemplate();
        String url= Main.server+ctrllerPath+"informatizationRequirementDetailsDTO/informatizationRequirementIndex?informatizationRequirementIndex="+informatizationRequirementIndex;
        ResponseEntity<M_requirementDetailsDto> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<M_requirementDetailsDto>() {}
        );
        return response.getBody();
    }

    public List<M_requirementBasicDto> getList4Like(M_requirementBasicDto temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+ctrllerPath+"basicDto4Like?"+tempString;
        ResponseEntity<List<M_requirementBasicDto>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_requirementBasicDto>>() {}
        );
        return response.getBody();
    }

    public Integer update(M_requirementDetailsDto temp) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"detailsDto";
        HttpEntity<M_requirementDetailsDto> request = new HttpEntity<>(temp, null);
        ResponseEntity<?> response = restTemplate.exchange(
                url,
                HttpMethod.PUT,
                request, // 或者使用 HttpEntity 来添加请求头等信息
                Integer.class
        );
        return (Integer) response.getBody();
    }

    public void saveDetails(M_requirementDetailsDto detailsDto) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"requirementDetailsDto";
        HttpEntity<M_requirementDetailsDto> request = new HttpEntity<>(detailsDto, null);
        restTemplate.exchange(
                url,
                HttpMethod.PUT,
                request, // 或者使用 HttpEntity 来添加请求头等信息
                Integer.class
        );
    }

    public Integer deleteDetailsDto(M_requirementDetailsDto temp) {
        if(temp!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"deleteDetailsDto";
            HttpEntity<M_requirementDetailsDto> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public M_requirementDetailsDto newDetailsDto(M_requirementDetailsDto temp) {
        if(temp!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"";
            HttpEntity<M_requirementDetailsDto> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    M_requirementDetailsDto.class
            );
            return (M_requirementDetailsDto) response.getBody();
        }
        return null;
    }

}
